﻿#include "ThunderElement.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Data/Stat.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    ThunderElement::ThunderElement(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void ThunderElement::CompleteAttack(std::vector<std::any> &data)
    {
        std::vector<MapObject*> targets = FindAllTargets(2, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        OriginalTarget = getTarget();
        for (int i = 0; i < targets.size(); i++)
        {
            setTarget(targets[i]);
            Attack();
        }

        setTarget(OriginalTarget);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void ThunderElement::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            DelayedAction tempVar(DelayedType::Damage, getEnvir()->getTime() + 500);
            ActionList.push_back(&tempVar);
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }

    void ThunderElement::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::MAC});
        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
    }

    int ThunderElement::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        if (type != DefenceType::Repulsion)
        {
            return 0;
        }

        return MonsterObject::Attacked(attacker, damage, type);
    }

    int ThunderElement::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        if (type != DefenceType::Repulsion)
        {
            return 0;
        }

        return MonsterObject::Attacked(attacker, damage, type, damageWeapon);
    }

    int ThunderElement::Pushed(MapObject *pusher, MirDirection dir, int distance)
    {
        int result = MonsterObject::Pushed(pusher, dir, distance);

        if (result > 0)
        {
            if (dynamic_cast<PlayerObject*>(pusher) != nullptr)
            {
                Attacked(static_cast<PlayerObject*>(pusher), std::max(50, getEnvir()->Random->Next(Stats[Stat::HP])), DefenceType::Repulsion);
            }
            else if (dynamic_cast<MonsterObject*>(pusher) != nullptr)
            {
                Attacked(static_cast<MonsterObject*>(pusher), std::max(50, getEnvir()->Random->Next(Stats[Stat::HP])), DefenceType::Repulsion);
            }
        }
        return result;
    }

    void ThunderElement::PoisonDamage(int amount, MapObject *Attacker)
    {
        return;
    }
}
